package com.nowcoder.community.controller;

import com.nowcoder.community.annotation.LoginRequired;
import com.nowcoder.community.entity.Event;
import com.nowcoder.community.entity.Page;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.event.EventProducer;
import com.nowcoder.community.service.FollowService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import com.nowcoder.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController {

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private FollowService followService;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(value = "/follow", method = RequestMethod.POST)
    @ResponseBody
    @LoginRequired
    public void follow(int targetId) {
        User loginUser = hostHolder.getUser();
        followService.follow(loginUser.getId(), targetId);

        if (followService.getFollowStatus(loginUser.getId(), targetId)) {
            // 触发关注主题的系统通知事件
            Event event = new Event()
                    .setTopic(CommunityConstant.TOPIC_FOLLOW)
                    .setUserId(loginUser.getId())
                    .setEntityType(CommunityConstant.ENTITY_TYPE_USER)
                    .setEntityId(targetId)
                    .setEntityUserId(targetId);
            eventProducer.produceEvent(event);
        }
    }

    @RequestMapping(value = "/idols/{userId}", method = RequestMethod.GET)
    public String getIdols(Model model, Page page, @PathVariable("userId") int userId) {
        User user = userService.findUserById(userId);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在！");
        }
        model.addAttribute("user", user);

        page.setRows((int) followService.getIdolCount(userId));
        page.setPath("/idols/" + userId);

        List<Map<String, Object>> idols = followService.getIdols(userId, page.getOffset(), page.getLimit());
        for (Map<String, Object> idol : idols) {
            User u = (User) idol.get("user");
            boolean hasFollowed = false;
            if (hostHolder.getUser() != null) {
                hasFollowed = followService.getFollowStatus(hostHolder.getUser().getId(), u.getId());
            }
            idol.put("hasFollowed", hasFollowed);
        }

        model.addAttribute("idols", idols);
        return "/site/idol";
    }

    @RequestMapping(value = "/followers/{userId}", method = RequestMethod.GET)
    public String getFollowers(Model model, Page page, @PathVariable("userId") int userId) {
        User user = userService.findUserById(userId);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在！");
        }
        model.addAttribute("user", user);

        page.setRows((int) followService.getFollowerCount(userId));
        page.setPath("/followers/" + userId);

        List<Map<String, Object>> followers = followService.getFollowers(userId, page.getOffset(), page.getLimit());
        for (Map<String, Object> follower : followers) {
            User u = (User) follower.get("user");
            boolean hasFollowed = false;
            if (hostHolder.getUser() != null) {
                hasFollowed = followService.getFollowStatus(hostHolder.getUser().getId(), u.getId());
            }
            follower.put("hasFollowed", hasFollowed);
        }

        model.addAttribute("followers", followers);
        return "/site/follower";
    }

}
